#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Created by master on 2017/3/6


import socketserver


class MyTCPHandle(socketserver.BaseRequestHandler):
    def handle(self):  # 这个方法必须有   所有与客户端交互的逻辑都在这里
        print("New conn:", self.client_address)
        while True:

            data = self.request.recv(1024)

            if not data: break
            print("Client Says:", data.decode())
            self.request.send(data)


if __name__ == '__main__':
    HOST, PORT = "localhost", 50007
    # 把刚才写的类当作一个参数传给ThreadingTCPServer这个类，下面的代码就创建了一个服务
    server = socketserver.ThreadingTCPServer((HOST, PORT), MyTCPHandle)
    # 启动这个服务  服务会一直运行  除非ctrl+c
    server.serve_forever()
